﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace CaptialVices
{
    ////*.o *.lo *.la #*# .*.rej *.rej .*~ *~ .#* .DS_Store thumbs.db Thumbs.db *.bak *.class *.exe *.dll *.mine *.obj *.ncb *.lib *.log *.idb *.pdb *.ilk *.msi* .res *.pch *.suo *.exp *.*~ *.~* ~*.* cvs CVS .CVS .cvs release Release debug Debug ignore Ignore bin Bin obj Obj *.csproj.user *.user ipch *.sdf *.opensdf
    public class AnimatedSprite
    {
        enum AnimationState
        {
            loop,
            forward,
            reverse,
            single
        };

        public Texture2D texture;

        Color backColor = Color.White;

        public Rectangle[] currentAnimFrames;
        Dictionary<string,Rectangle[]> Animations;

        AnimationState animState = AnimationState.loop;

        public bool reverse = false;

        public float scale;

        public int FrameNum
        {
            get;
            set;
        }

        public double FrameTime
        {
            get;
            set;
        }

        public string[] AnimKeys
        {
            get
            {
                return Animations.Keys.ToArray();
            }
        }

        public int Width
        {
            get { return (int)(currentAnimFrames[0].Width * scale); }
            set { }
        }

        public int Height
        {
            get { return (int)(currentAnimFrames[0].Height * scale); }
            set { }
        }

        public string currentAnimation="";

        public void LoopAnimation(string value)
        {
            if (Animations.Keys.Contains(value) && !currentAnimation.Equals(value))
            {
                currentAnimFrames = Animations[value];
                FrameNum = 0;
                //FrameTime = .1;
                animState = AnimationState.loop;
                currentAnimation = value;
            }
        }

        public void ForwardAnimation(string value)
        {
            if (Animations.Keys.Contains(value))
            {
                currentAnimFrames = Animations[value];
                FrameNum = 0;
                //FrameTime = 3;
                animState = AnimationState.forward;
                currentAnimation = value;
            }
        }

        public void ReverseAnimation(string value)
        {
            if (Animations.Keys.Contains(value))
            {
                currentAnimFrames = Animations[value];
                FrameNum = currentAnimFrames.Length-1;
                FrameTime = 3;
                animState = AnimationState.reverse;
                currentAnimation = value;
            }
        }

        public void SingleFrameDraw(string value)
        {
            if (Animations.Keys.Contains(value))
            {
                currentAnimFrames = Animations[value];
                FrameNum = 0;
                FrameTime = 100000;
                animState = AnimationState.single;
                currentAnimation = value;
            }
        }

        public AnimatedSprite(Texture2D tex)
        {
            texture = tex;
            currentAnimFrames = new Rectangle[1];
            currentAnimFrames[0] = new Rectangle(0, 0, texture.Width, texture.Height);

            Initialize();
        }

        public AnimatedSprite(Texture2D tex, Rectangle[] frames)
        {
            texture = tex;
            this.currentAnimFrames = frames;

            Initialize();
        }

        public AnimatedSprite(Texture2D tex, Dictionary<string, Rectangle[]> animations)
        {
            texture = tex;
            this.Animations = animations;
            currentAnimFrames = Animations.Values.First();

            Initialize();
        }

        private void Initialize()
        {
            FrameNum = 0;
            FrameTime = 100000;  //arbitrarily large number to get no animation.
            scale = 1.0f;
        }

        double timeSinceFrameSwap = 0;


        public void Draw(GameTime gametime, SpriteBatch spritebatch, Rectangle destination, Vector2 origin, float rotation, Color color, float layerDepth)
        {
            spritebatch.Draw(texture, destination, currentAnimFrames[FrameNum], backColor, rotation, origin, reverse ? SpriteEffects.FlipHorizontally : SpriteEffects.None, layerDepth);
            timeSinceFrameSwap += gametime.ElapsedGameTime.TotalSeconds;

            if (animState != AnimationState.single)
            {
                //Console.WriteLine("{0} {1}", timeSinceFrameSwap, FrameTime);
                if (timeSinceFrameSwap > FrameTime)
                {
                    if (animState == AnimationState.forward && FrameNum != currentAnimFrames.Length - 1)
                    {
                        timeSinceFrameSwap = 0;
                        FrameNum = ++FrameNum % currentAnimFrames.Length;
                    }
                    else if (animState == AnimationState.reverse && FrameNum != 0)
                    {
                        timeSinceFrameSwap = 0;
                        FrameNum = --FrameNum % currentAnimFrames.Length;
                    }
                    else
                    {
                        timeSinceFrameSwap = 0;
                        FrameNum = ++FrameNum % currentAnimFrames.Length;
                    }
                }
            }
        }       
    }
}
